package com.pro.framework.message;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.pro.framework.api.FrameworkConst;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
public class MessageConfig {

    /**
     * 消息专用的RedisTemplate - 明确不带类型信息
     */
    @Bean
    @Qualifier(FrameworkConst.RedisTemplate.messageRedisTemplate)
    public RedisTemplate<String, Object> messageRedisTemplate(RedisConnectionFactory factory,
                                                              @Qualifier(FrameworkConst.JsonObjectMapper.messageObjectMapper) ObjectMapper messageMapper) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(factory);

        // 消息专用ObjectMapper - 不带类型信息
        Jackson2JsonRedisSerializer<Object> serializer =
                new Jackson2JsonRedisSerializer<>(messageMapper, Object.class);

        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(serializer);
        template.setHashValueSerializer(serializer);

        return template;
    }

}
